﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ALMDDD.Domain.Core.Notifications
{
    /// <summary>
    /// 消息通知处理程序
    /// </summary>
    public class DomainNotificationHandler : INotificationHandler<DomainNotification>
    {
        /// <summary>
        /// 通知信息列表
        /// </summary>

        public List<DomainNotification> _notifications;
        public DomainNotificationHandler()
        {
            _notifications = new List<DomainNotification>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task Handle(DomainNotification notification, CancellationToken cancellationToken)
        {
            _notifications.Add(notification);
            return Task.CompletedTask;
        }
        public virtual List<DomainNotification> GetNotifications()
        {
            return _notifications;
        }
        /// <summary>
        /// 判断当前总线对象周期中，是否存在通知信息
        /// </summary>
        /// <returns></returns>
        public virtual bool HasNotifications()
        {
            return GetNotifications().Any();
        }

        // 手动回收（清空通知）
        public void Dispose()
        {
            _notifications = new List<DomainNotification>();
        }

    }
}
